package com.yoso.file;

import com.alibaba.excel.annotation.ExcelProperty;
import com.yoso.domain.SecInfo;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Test;

import java.io.*;
import java.lang.reflect.Field;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

/**
 * @author shizejin
 * @date 2022/1/20
 * @description:
 */
public class FileRead {
    /**
     * readFile
     */
    @Test
    public void testRead() {
        String filePath = "C:\\Users\\12425\\Desktop\\____\\品种信息维护\\彭博文件\\ BatchSecurity.csv";
        Path path = Paths.get(filePath);
        File file = path.toFile();
        List<SecInfo> secInfoList = parseFileToList(file, SecInfo.class);
        secInfoList.forEach(System.out::println);
    }

    @SneakyThrows
    public <T> List<T> parseFileToList(File file, Class<T> tClass) {
        List<T> result = new ArrayList<>();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
        Field[] fields = tClass.getDeclaredFields();
        bufferedReader.lines().forEach(line -> {
            try {
                T obj = tClass.newInstance();
                String[] value = line.split(",");
                for (Field field : fields) {
                    ExcelProperty annotation = field.getAnnotation(ExcelProperty.class);
                    int index = annotation.index();
                    field.setAccessible(true);
                    field.set(obj, value[index]);
                    field.setAccessible(false);
                }
                result.add(obj);
            } catch (InstantiationException | IllegalAccessException e) {
                e.printStackTrace();
            }
        });
        return result;
    }

    /**
     *
     */
    @Test
    public void test() {
        String filePath = "C:\\Users\\12425\\Desktop\\____\\品种信息维护\\彭博文件\\BatchSecurity.csv";
        String filePath2 = "C:\\Users\\12425\\Desktop\\____\\品种信息维护\\彭博文件\\BatchSecurity2.csv";
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath)));
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath2)));

            reader.lines().forEach(s -> {
                try {
                    writer.write(s);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
            writer.flush();
            writer.close();
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
    * 2
    */
    @SneakyThrows
    @Test
    public void test2(){
        String gbkPath = "C:\\Users\\12425\\Desktop\\新建文件夹\\GBK.txt";
        String u8Path = "C:\\Users\\12425\\Desktop\\新建文件夹\\u8.txt";
        String path = u8Path;
        File file = new File(path);
        InputStream in = new FileInputStream(file);
        OutputStream out = new FileOutputStream(path + ".bak");
        for (int i = 0; i < 6; i++) {
            int read = in.read();
            out.write(read);
            System.out.println("read = " + read);
        }
        byte[] arr =  new byte[]{};
        String a = "b";
        char c = '一';
        in.close();
        out.close();
    }
    
    /**
    * 3
    */
    @Test
    public void test3(){
        Integer s = null;
        if (s == 2) {
            System.out.println("s = " + s);
        }
    }
}
